package com.ruoyi.system.service.impl.estate;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.estate.CollectionMapper;
import com.ruoyi.system.domain.estate.UserCollection;
import com.ruoyi.system.service.estate.ICollectionService;
import com.ruoyi.common.utils.DateUtils;

/**
 * 收藏Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class CollectionServiceImpl implements ICollectionService 
{
    @Autowired
    private CollectionMapper collectionMapper;

    /**
     * 查询收藏
     * 
     * @param collectionId 收藏主键
     * @return 收藏
     */
    @Override
    public UserCollection selectCollectionByCollectionId(Long collectionId)
    {
        return collectionMapper.selectCollectionByCollectionId(collectionId);
    }

    /**
     * 查询收藏列表
     * 
     * @param collection 收藏
     * @return 收藏
     */
    @Override
    public List<UserCollection> selectCollectionList(UserCollection collection)
    {
        return collectionMapper.selectCollectionList(collection);
    }

    /**
     * 新增收藏
     * 
     * @param collection 收藏
     * @return 结果
     */
    @Override
    public int insertCollection(UserCollection collection)
    {
        collection.setCreateTime(DateUtils.getNowDate());
        return collectionMapper.insertCollection(collection);
    }

    /**
     * 修改收藏
     * 
     * @param collection 收藏
     * @return 结果
     */
    @Override
    public int updateCollection(UserCollection collection)
    {
        collection.setUpdateTime(DateUtils.getNowDate());
        return collectionMapper.updateCollection(collection);
    }

    /**
     * 批量删除收藏
     * 
     * @param collectionIds 需要删除的收藏主键
     * @return 结果
     */
    @Override
    public int deleteCollectionByCollectionIds(Long[] collectionIds)
    {
        return collectionMapper.deleteCollectionByCollectionIds(collectionIds);
    }

    /**
     * 删除收藏信息
     * 
     * @param collectionId 收藏主键
     * @return 结果
     */
    @Override
    public int deleteCollectionByCollectionId(Long collectionId)
    {
        return collectionMapper.deleteCollectionByCollectionId(collectionId);
    }
    
    /**
     * 检查是否已收藏
     * 
     * @param userId 用户ID
     * @param collectionType 收藏类型
     * @param targetId 目标ID
     * @return 结果
     */
    @Override
    public boolean checkCollection(Long userId, String collectionType, Long targetId)
    {
        UserCollection collection = collectionMapper.checkCollection(userId, collectionType, targetId);
        return collection != null;
    }
    
    /**
     * 检查是否已收藏（包括已删除的记录）
     * 
     * @param userId 用户ID
     * @param collectionType 收藏类型
     * @param targetId 目标ID
     * @return 收藏对象
     */
    @Override
    public UserCollection checkCollectionIncludeDeleted(Long userId, String collectionType, Long targetId)
    {
        return collectionMapper.checkCollectionIncludeDeleted(userId, collectionType, targetId);
    }
    
    /**
     * 添加收藏
     * 
     * @param userId 用户ID
     * @param collectionType 收藏类型
     * @param targetId 目标ID
     * @return 结果
     */
    @Override
    public int addCollection(Long userId, String collectionType, Long targetId)
    {
        // 检查是否已存在记录（包括已删除的）
        UserCollection existingCollection = checkCollectionIncludeDeleted(userId, collectionType, targetId);
        
        if (existingCollection != null)
        {
            // 如果存在记录，则更新它
            existingCollection.setDelFlag("0");
            existingCollection.setStatus("0");
            existingCollection.setCreateTime(DateUtils.getNowDate());
            return collectionMapper.updateCollection(existingCollection);
        }
        else
        {
            // 如果不存在记录，则插入新记录
            UserCollection collection = new UserCollection();
            collection.setUserId(userId);
            collection.setCollectionType(collectionType);
            collection.setTargetId(targetId);
            collection.setCreateTime(DateUtils.getNowDate());
            collection.setStatus("0");
            collection.setDelFlag("0");
            return collectionMapper.insertCollection(collection);
        }
    }
    
    /**
     * 取消收藏
     * 
     * @param userId 用户ID
     * @param collectionType 收藏类型
     * @param targetId 目标ID
     * @return 结果
     */
    @Override
    public int cancelCollection(Long userId, String collectionType, Long targetId)
    {
        return collectionMapper.cancelCollection(userId, collectionType, targetId);
    }
    
    /**
     * 查询用户收藏的资讯列表
     * 
     * @param userId 用户ID
     * @return 收藏集合
     */
    @Override
    public List<UserCollection> selectNewsCollectionList(Long userId)
    {
        return collectionMapper.selectNewsCollectionList(userId);
    }
    
    /**
     * 查询用户收藏的公告列表
     * 
     * @param userId 用户ID
     * @return 收藏集合
     */
    @Override
    public List<UserCollection> selectNoticeCollectionList(Long userId)
    {
        return collectionMapper.selectNoticeCollectionList(userId);
    }
} 